package com.duanjw;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import javax.servlet.ServletException;

public class MySpringBootApplication {
	public static void main(String[] args) throws ClassNotFoundException, LifecycleException, ServletException {
		System.out.println("启动tomcat");
		Tomcat tomcat = new Tomcat();
		tomcat.setPort(8081);
		String path = MySpringBootApplication.class.getResource("/").getPath();
		Context context = tomcat.addWebapp("", "/Users/duanjw/webapps");

		WebResourceRoot standardRoot = new StandardRoot(context);
		standardRoot.addPreResources(new DirResourceSet(standardRoot,"/WEB-INFO/classes",path,"/"));

		context.setResources(standardRoot);
		// Tomcat 9.0 必须调用 Tomcat#getConnector() 方法之后才会监听端口
		tomcat.getConnector();
		tomcat.start();
		tomcat.getServer().await();
	}
}
